﻿using LightCAD.Core;
using LightCAD.Three;
using QdArch.ThreeUtils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QdArch
{
    public partial class DoorAction
    {
        public override BufferGeometry[] CreateGeometry3D(LcElement element, Matrix3d matrix)
        {
            var door = element as QdDoorRef;
            var isLeft = door.IsLeft;
            var bottom = door.Bottom;
            var start = (door.StartPoint).ToThree(bottom);
            var end = (door.EndPoint).ToThree(bottom);
            var height = door.Height;
            var thickness = door.Thickness;
            var thicknessHlf = thickness / 2;
            var lineVec = new Vector3().subVectors(end, start);
            var len = lineVec.length();
            var xAxis = lineVec.clone().normalize();
            var yAxis = VectorUtil.ZAxis.clone();
            var zAxis = new Vector3().crossVectors(xAxis, yAxis).normalize();
            var coodMat = new Matrix4();
            coodMat.makeBasis(xAxis, yAxis, zAxis);
            coodMat.setPosition(start);
            var shape = ShapeUtil.Rect(0, 0, len, height);
            var handleLoc = isLeft ? len - 100 : 100;
            var handleShape = ShapeUtil.Hexagon(handleLoc, height / 2, 50);
            var doorGeoData= GeoUtil.GetStretchGeometryData(shape, coodMat, thicknessHlf,-thicknessHlf);
            var handleGeoData= GeoUtil.GetStretchGeometryData(handleShape, coodMat, thicknessHlf+50,-thicknessHlf- 50);
            var geoDoor = doorGeoData.GetBufferGeometry();
            geoDoor.computeVertexNormals();
            var geoHandle = handleGeoData.GetBufferGeometry();
            geoHandle.computeVertexNormals();
            return new BufferGeometry[] { geoDoor, geoHandle };
        }
    }
}
